class AuthorsController < ApplicationController

  def index
    @authors_grid = initialize_grid(Author, per_page: 10)
  end

  def new
    @author = Author.new
  end

  def create
    @author = Author.new post_params
    if @author.save
      redirect_to :action => 'show', :id => @author.id
    else
      render :action => 'new'
    end
  end

  def edit
    @author = Author.find params[:id]
  end

  def update
    #render json: params.to_json
    @author = Author.find params[:id]
    if @author.update_attributes post_params
      redirect_to :action => 'show', :id => @author.id
    else
      render :action => :edit
    end
  end

  def destroy
    @author = Author.find params[:id]
    @author.books.destroy
    @author.destroy
    redirect_to authors_path
  end

  def show
    @author = Author.find params[:id]
    @books_grid = initialize_grid(@author.books, per_page: 10)
  end
  
  private
  
  def post_params
    params.require(:author).permit(:name, :phone, :email, :address, :zip, :country, :accepts, :foo, :foo_confirmation)
  end
  
end